package com.meilele.controller.shiro;


import com.meilele.biz.shiro.BaseBiz;
import com.meilele.entity.shiro.User;
import com.meilele.util.zhengchao.Contants;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.PrintWriter;

@Controller
@RequestMapping("/shiro")
public class baseController {
    @Resource
    private BaseBiz baseBiz;
    @RequestMapping("/login")
    public void login(String username, String password, PrintWriter out, HttpSession session){
        try {
            StringBuffer sb=new StringBuffer();
            String pwd= sb.append(Contants.SALT).append(password).toString();
            baseBiz.login(username,pwd);
            User user=baseBiz.findByUsernameAndPassword(username,pwd);
            session.setAttribute("user",user);
            out.print("success");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @RequestMapping("/preIndex")
    public String preIndex(){
        return "index";
    }
    @RequestMapping("/preRegirect")
    public String preRegirect(){
        return "regirect";
    }
    @RequestMapping("/preLogin")
    public String preLogin(){
        return "login";
    }
    @RequestMapping("/checkYZM")
    public void checkYZM(String yanZM,PrintWriter out,HttpSession session){
      String yanzm = (String)session.getAttribute("yanZM");
      if (yanZM.equalsIgnoreCase(yanzm)){
          out.write("success");
      }else{
          out.write("fail");
      }
    }
    @RequestMapping("/regirect")
    public String saveUser(User user){
        baseBiz.saveUser(user);
        return "redirect:/shiro/preLogin    ";
    }
    @RequestMapping("/preManagerLogin")
    public String preManagerLogin(){
        return "managerLogin";
    }

    @RequestMapping("/managerLogin")
    public void managerLogin(String username, String password, PrintWriter out, HttpSession session){
        try {
            StringBuffer sb=new StringBuffer();
            String pwd= sb.append(Contants.SALT).append(password).toString();
            baseBiz.login(username,pwd);
            User user=baseBiz.findByUsernameAndPassword(username,pwd);
            session.setAttribute("user",user);
            out.print("success");
        } catch (Exception e) {
            out.print("ss");
            e.printStackTrace();
        }
    }

}
